package com.xge.common_libary.http

import com.xge.common_libary.api.ApiService
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

object RetrofitClient {
    private const val BASE_URL = "https://jsucpdq.market.alicloudapi.com/"

    // ApiClient.kt 或 NetworkModule.kt，根据你的项目结构命名
    val apiService: ApiService by lazy {
        val client = OkHttpClient.Builder()
            .addInterceptor { chain ->
                val original = chain.request()
                val requestBuilder = original.newBuilder()

                // 引入 HeaderProvider 提供的头部
                HeaderProvider.getHeaders().forEach { (key, value) ->
                    requestBuilder.header(key, value)
                }

                val request = requestBuilder
                    .method(original.method, original.body)
                    .build()

                chain.proceed(request)
            }
            .build()

        Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(client)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(ApiService::class.java)
    }

}